package com.yhzdys.ocr.service.support;

import ai.djl.modality.cv.Image;
import lombok.extern.slf4j.Slf4j;
import org.opencv.core.Mat;

@Slf4j
public class OcrUtils {

    public static int[] resize32(double h, double w) {
        double min = Math.min(h, w);
        if (min < 32) {
            h = 32.0 / min * h;
            w = 32.0 / min * w;
        }
        int h32 = (int) h / 32;
        int w32 = (int) w / 32;
        return new int[]{h32 * 32, w32 * 32};
    }

    public static void releaseImage(Image image) {
        if (image == null) {
            return;
        }
        Object wrappedImage = image.getWrappedImage();
        if (!(wrappedImage instanceof Mat)) {
            return;
        }
        try {
            ((Mat) wrappedImage).release();
        } catch (Throwable t) {
            log.error("Release image error", t);
        }
    }
}
